package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.pojo.EhyModule;
import com.cqut.hfh.ordering.pojo.EhyRole;
import com.cqut.hfh.ordering.service.IModuleService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/24-13:10
 */
@RequestMapping("/module")
@Controller
public class ModuleController {


    @Resource
    private IModuleService moduleService;


    /**
     * 返回所有模块
     * @return
     */
    @RequestMapping(path = "/findAll", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findAll(@RequestParam("limit") String limit,
                          @RequestParam("page") String page){
        List<Map<String, Object>> result_list = moduleService.getModules(limit, page);

        for( Map<String, Object> map : result_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = moduleService.getCount();

        return Message.getReturnMsg(0,"", count, result_list);
    }


    /**
     * 删除指定ID模块
     * @return
     */
    @RequestMapping(path = "/delete", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String delete(@RequestParam("id")String mod_id){
        int a = moduleService.deleteById(mod_id);
        return Message.getReturnMsg(0,"ok");
    }



    /**
     * 根据名字查模块
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findModulesByName", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findModulesByName(@RequestParam("limit") String limit,
                                    @RequestParam("page") String page,
                                    @RequestParam("mod_name") String mod_name){

        List<Map<String, Object>> mod_list = moduleService.getModulesByName(limit, page, mod_name);

        for( Map<String, Object> map : mod_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = mod_list.size();

        return Message.getReturnMsg(0,"", count, mod_list);
    }

    /**
     * 添加更新并模块
     * @param module
     * @return
     */
    @RequestMapping(path = "/saveOrUpdate", produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String saveOrUpdate(EhyModule module){

        module.setOptime(new Date());

        if( module.getMod_id() == null){
            module.setMod_id(EntityIdutils.getId());
            int temp = moduleService.saveModule(module);
            return Message.getReturnMsg(0,"ok");
        }else{
            int temp = moduleService.updateModule(module);
        }
        return Message.getReturnMsg(0,"ok");
    }

    /**
     *  根据模板ID查询信息
     * @param mod_id
     * @return
     */
    @PostMapping("/findById")
    @ResponseBody
    public EhyModule findById(@RequestParam("id")String mod_id){
        return moduleService.findById(mod_id);
    }


}
